package com.yiren.dbaa.module.practice_set.site.presenter;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import com.google.gson.Gson;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.module.practice_set.PracticeCarSetActivity;
import com.yiren.dbaa.module.practice_set.site.bean.ModifyValBean;
import com.yiren.dbaa.module.practice_set.site.contract.ReleaseContract;
import com.yiren.dbaa.module.practice_set.site.contract.SiteRuleContract;
import com.yiren.dbaa.module.practice_set.site.contract.VersionHistoryContract;
import com.yiren.dbaa.module.practice_set.site.model.ReleaseModel;
import com.yiren.dbaa.module.practice_set.site.model.SiteRuleModel;
import com.yiren.dbaa.module.practice_set.site.model.VersionHistoryModel;
import com.yiren.dbaa.module.practice_set.site.view.activity.SiteManageActivity;
import com.yiren.dbaa.module.project_edit.view.activity.ProjectManageActivity;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.List;

/**
 * @author lwb
 * @date : 2022/10/17 11:34
 * @description: 历史版本P
 */
public class VersionHistoryPresenter implements VersionHistoryContract.Presenter {

    private Activity activity;
    private VersionHistoryContract.Model versionHistoryModel;
    private SiteRuleContract.Model siteRuleModel;
    private ReleaseContract.Model releaseModel;
    private UserParamConfigBean userParamConfigBean;

    public VersionHistoryPresenter(Activity activity) {
        this.activity = activity;
        versionHistoryModel = new VersionHistoryModel(activity);
        siteRuleModel = new SiteRuleModel(activity);
        releaseModel = new ReleaseModel(activity);
        userParamConfigBean = LoadConfigApi.loadUserParamConfig(activity);
    }

    @Override
    public void loadVersionList(VersionHistoryContract.View view, int userId, String siteCode) {
        LoadDialog.show(activity, "正在加载数据...");
        versionHistoryModel.getVersionList(userId, siteCode, new CommonCallBack() {
            @Override
            public void success(Object result) {
                LoadDialog.dismiss(activity);
                List<SiteEntity> siteEntityList = (List<SiteEntity>) result;
                view.showVersionList(siteEntityList);
            }

            @Override
            public void fail(String error) {
                LoadDialog.dismiss(activity);
                MessageBox.show(activity, error);
            }
        });


    }

    @Override
    public void downLoadSiteRuleData(SiteEntity siteEntity, VersionHistoryContract.View view) {
        activity.runOnUiThread(() -> LoadDialog.show(activity, "正在下载中..."));
        //下载基础模板数据
        siteRuleModel.downLoadBaseData(siteEntity, new CommonCallBack() {
            @Override
            public void success(Object result) {
                super.success(result);
                //下载修改数据
                siteRuleModel.downLoadModifyVal(userParamConfigBean.getUserInfo().getId(), siteEntity.getSiteCode(), siteEntity.getRuleVer(), new CommonCallBack() {
                    @Override
                    public void success(Object result) {
                        super.success(result);
                        //本地数据插入
                        ModifyValBean modifyValBean = (ModifyValBean) result;
                        siteEntity.setPartneModifyVal(modifyValBean.getPartneModifyVal());
                        siteEntity.setCoachModifyVal(modifyValBean.getCoachModifyVal());
                        UserDbController.getInstance(false).insertOrReplace(siteEntity);
                        //通知视图
                        LoadDialog.dismiss(activity);
                        CommonDialog.showTwoBtn(activity, "操作确认", "下载已完成，请问是直接发布，还是编辑规则？", "直接发布", "编辑规则", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (which == -2) {
                                    releaseSiteRule(siteEntity);
                                } else {
                                    Intent intent = new Intent(activity, ProjectManageActivity.class);
                                    Bundle bundle = new Bundle();
                                    bundle.putSerializable("site", siteEntity);
                                    intent.putExtras(bundle);
                                    activity.startActivity(intent);
                                    activity.finish();
                                }
                                dialog.dismiss();
                            }
                        });
                    }

                    @Override
                    public void fail(String error) {
                        super.fail(error);
                    }
                });
            }

            @Override
            public void fail(String error) {
                super.fail(error);
            }
        });

    }

    @Override
    public void releaseSiteRule(SiteEntity site) {
        releaseModel.publishRule(site, userParamConfigBean.getUserInfo().getId().toString(), site.getUpdateDetails(), site.getUpdateMode(), new CommonCallBack() {
            @Override
            public void success(Object result) {
                site.setRuleVer(site.getRuleVerLast() + 1);
                UserDbController.getInstance(false).update(site);
                LoadDialog.dismiss(activity);
                MessageBox.MessageAndVoiceModel(activity, "考场规则发布成功!");
                Intent intent = new Intent(activity, PracticeCarSetActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("site", site);
                intent.putExtras(bundle);
                activity.startActivity(intent);
            }

            @Override
            public void fail(String error) {
                activity.runOnUiThread(() -> CommonDialog.showOneBtn(activity, "操作提示", "考场规则数据发布失败!", "我知道了", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }));
            }
        });
    }
}
